/* Copyright Thanh-Trung Pham, 2015 */

package jp.ac.uaizu.control;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.ProtectionDomain;
import java.util.Properties;

/**
 * @author Thanh-Trung Pham
 */
public class SerCompSettings {
    private static final String SETTINGS_FILENAME = "RMPISerComp.properties";


    private static final String DEFAULT_TRANSLATORS_FOLDER_NAME = "translators";
    private static final String DEFAULT_GENERATORS_FOLDER_NAME = "generators";

    private static final int DEFAULT_LISTENING_PORT = 8989;
    private static final String DEFAULT_LISTENING_PORT_AS_STRING = "8989";

    private static SerCompSettings m_oSerCompSettings;

    private int m_iListeningPort;
    private String m_strTranslatorsFolderName;
    private String m_strGeneratorsFolderName;


    /**
     * The constructor.
     */
    private SerCompSettings() {
        load();
    }

    /**
     * Get the only instance.
     *
     * @return the only instance.
     */
    public static synchronized SerCompSettings getInstance() {
        if (m_oSerCompSettings == null) {
            m_oSerCompSettings = new SerCompSettings();
        }

        return m_oSerCompSettings;
    }

    /**
     * Get the listening port.
     *
     * @return the listening port.
     */
    public int getListeningPort() {
        return m_iListeningPort;
    }

    /**
     * Get the translators' folder name.
     *
     * @return the translators' folder name.
     */
    public String getTranslatorsFolderName() {
        return m_strTranslatorsFolderName;
    }

    /**
     * Get the generators' folder name.
     *
     * @return the generators' folder name.
     */
    public String getGeneratorsFolderName() {
        return m_strGeneratorsFolderName;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

    /**
     * Get the current working directory.
     * @return the current working directory.
     * @throws URISyntaxException if there is an error.
     */
    private String getCurrentWorkingDirectory(){
        String strCurrentWorkingDir = System.getProperty("user.dir");

        try {
            ProtectionDomain oProtectionDomain = getClass().getProtectionDomain();
            URL oLoadedLocation = oProtectionDomain.getCodeSource().getLocation();
            String strLoadedPath = oLoadedLocation.toURI().getSchemeSpecificPart();
            File oFile = new File(strLoadedPath);

            if (oFile.isFile()) {
                //loaded from a jar file
                return oFile.getParent();
            }

            return strCurrentWorkingDir;
        } catch (URISyntaxException oEx) {
            oEx.printStackTrace();
            return strCurrentWorkingDir;
        }
    }

    /**
     * The loader.
     */
    private void load() {
        Properties oSettings = new Properties();
        try {
            oSettings.load(new FileInputStream(getCurrentWorkingDirectory() + File.separator + SETTINGS_FILENAME));

            loadListeningPortSetting(oSettings);
            loadTranslatorsFolderSetting(oSettings);
            loadGeneratorsFolderSetting(oSettings);
        } catch (IOException oEx) {
            oEx.printStackTrace();
            loadDefaultRunConfigurationSetting();
        }
    }

    /**
     * Load default run configuration settings.
     */
    private void loadDefaultRunConfigurationSetting() {
        m_iListeningPort = DEFAULT_LISTENING_PORT;
        m_strGeneratorsFolderName = DEFAULT_GENERATORS_FOLDER_NAME;
        m_strTranslatorsFolderName = DEFAULT_TRANSLATORS_FOLDER_NAME;
    }


    /**
     * Load the generators folder setting.
     *
     * @param settings the settings.
     */
    private void loadGeneratorsFolderSetting(Properties settings) {
        m_strGeneratorsFolderName = settings.getProperty("generators.folder", DEFAULT_GENERATORS_FOLDER_NAME);
        File oGeneratorsFolder = new File(m_strGeneratorsFolderName);

        if(!oGeneratorsFolder.isAbsolute()){
            m_strGeneratorsFolderName = getCurrentWorkingDirectory() + File.separator + m_strGeneratorsFolderName;
        }
    }

    /**
     * Load the translators folder setting.
     *
     * @param settings the settings.
     */
    private void loadTranslatorsFolderSetting(Properties settings) {
        m_strTranslatorsFolderName = settings.getProperty("translators.folder", DEFAULT_TRANSLATORS_FOLDER_NAME);
        File oTranslatorsFolder = new File(m_strTranslatorsFolderName);

        if(!oTranslatorsFolder.isAbsolute()){
            m_strTranslatorsFolderName = getCurrentWorkingDirectory() + File.separator + m_strTranslatorsFolderName;
        }
    }

    /**
     * Load the listening port setting.
     *
     * @param settings the settings.
     */
    private void loadListeningPortSetting(Properties settings) {
        try {
            m_iListeningPort = Integer.parseInt(settings.getProperty("listening.port", DEFAULT_LISTENING_PORT_AS_STRING));
            if (m_iListeningPort <= 0 || m_iListeningPort > 65535) {
                m_iListeningPort = DEFAULT_LISTENING_PORT;
            }
        } catch (NumberFormatException oEx) {
            m_iListeningPort = DEFAULT_LISTENING_PORT;
        }
    }
}
